<template>
    <el-form :model="userinfo" :rules="rules" ref="loginForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
            <el-input v-model="userinfo.username"></el-input>
        </el-form-item>
         <el-form-item label="密码" prop="password">
            <el-input v-model="userinfo.password"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">{{$t('login')}}</el-button>
            <el-button @click="resetForm('ruleForm')">{{$t('reset')}}</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
import {userRules} from '@/config/rule.config';
import {mapActions} from 'vuex';
export default {
    data(){
        return {
            rules:userRules,
            userinfo:{
                username:'',
                password:''
            }
        }
    },
    methods:{
        ...mapActions('user',['login']),
        submitForm() {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    //登录
                    // this.$store.dispatch('login')
                    this.login(this.userinfo).then(res => {
                        if(res.code === 1){
                            this.$message({
                                message: res.msg,
                                type: 'success'
                            });
                            let prev = this.$route.query.prev ? this.$route.query.prev : '/main';
                            this.$router.replace(prev);
                        }else{
                            this.$message.error(res.msg);
                        }
                    })
                    alert('submit!');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(){
            this.$refs.loginForm.resetFields();
        }
    }
}
</script>